package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.BaseContext;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = service.getOne(lqw);
        if (emp == null) {
            return R.error("登录失败..");
        }
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败..");
        }
        if (emp.getStatus() == 0) {
            return R.error("该账号已被禁用..");
        }
        HttpSession session = request.getSession();
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        return R.success("退出成功..");
    }

    @PostMapping
    public R<Employee> addEmployee(@RequestBody Employee employee, HttpServletRequest request){
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        HttpSession session = request.getSession();

        Long empID = (Long) session.getAttribute("employee");
        BaseContext.setCurrentId(empID);

//        employee.setCreateUser(empID);
//        employee.setUpdateUser(empID);

        service.save(employee);

        return R.success(employee);
    }

    @GetMapping("/page")
    public R<Page> selectPage(Integer page,Integer pageSize,String name){
        log.info("当前页：{}，每页大小：{}，用户名：{}",page,pageSize,name);
        Page pageInfo = new Page();
        pageInfo.setPages(page);
        pageInfo.setSize(pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Employee::getUsername,name);
        service.page(pageInfo, lqw);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee, HttpServletRequest request){
        Long empID = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(empID);
        service.updateById(employee);
        return R.success("修改成功..");
    }

    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){
        Employee emp = service.getById(id);
        return R.success(emp);
    }

}
